<template>
	<!-- 我的发布 -->
	<view class="my-publish">
		<!-- tab切换 -->
		<uni-segmented-control :current="current" :values="items" :style-type="styleType" :active-color="activeColor"
			@clickItem="onClickItem" />
		<view class="content">
			<!-- 加载中效果 -->
			<uni-load-more v-if="loading" status="loading" />
			<!-- 人才自荐 -->
			<rczj v-if="!loading && rczjData.length > 0" idmypublish="true" :rczjData="rczjData" @on-edit="handleEdit"
				@on-delete="handleDelete"></rczj>
			<!-- 需求广场 -->
			<khzy v-if="!loading && khzyData.length > 0" idmypublish="true" :khzyData="khzyData" @on-edit="handleEdit"
				@on-delete="handleDelete" @on-ycj="handleYcj" @on-tj="handleTj"></khzy>
			<!-- 论坛 -->
			<lun-tan v-if="!loading && ltData.length > 0" idmypublish="true" :ltData="ltData" @on-edit="handleEdit"
				@on-delete="handleDelete"></lun-tan>

			<!-- 暂无数据显示 -->
			<view class="zwxx-img" v-if="!loading && showEmptyState">
				<image src="https://luntan.xiaouyf.cn/uecs-wechat/static/img/qianduan/zwxx.png" mode="widthFix"></image>
				<view>暂无数据</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Rczj from '../../components/Rczj.vue'; // 人才自荐
	import Khzy from '../../components/Khzy.vue'; // 需求广场
	import LunTan from '../../components/LunTan.vue'; // 论坛
	export default {
		components: {
			Rczj,
			Khzy,
			LunTan
		},
		data() {
			return {
				items: ['审核中', '展示中', '已驳回'],
				current: 0,
				styleType: 'text',
				activeColor: '#AC6C17',
				// releaseStatus 0:审核中 1:展示中 2:驳回
				releaseType: 0, // 0人才自荐 1需求广场 2 论坛
				rczjData: [], // 人才自荐
				khzyData: [], // 需求广场
				ltData: [], // 论坛
				loading: false, // 新增加载状态
			}
		},
		computed: {
			userInfo() {
				return this.$store.state.user.userInfo;
			},
			// 新增计算属性判断是否显示空状态
			showEmptyState() {
				return this.rczjData.length === 0 && this.khzyData.length === 0 && this.ltData.length === 0;
			}
		},
		onLoad(options) {
			// 如果是从人才自荐跳转过来的，默认选中展示中
			if (options && options.isRczj === 'true') {
				this.current = 1; // 1对应展示中
			}
			this.init()
		},
		methods: {
			async init() {
				this.loading = true; // 开始加载
				try {
					const data = {
						userId: this.userInfo.guid,
						releaseStatus: this.current
					};
					const res = await this.$api.getReleaseinfo(data);
					
					// 无论数据是否为空，都先清空现有数据
					this.rczjData = [];
					this.khzyData = [];
					this.ltData = [];
					
					if (res.succ && res.data) {
						let dataArray = res.data;
						// 过滤掉已删除的数据
						dataArray = dataArray.filter(item => item.releaseStatus !== '已删除');
						
						if (dataArray.length > 0) {
							// 根据releaseType分类数据
							this.rczjData = dataArray.filter(item => item.releaseType === '0')
								.sort((a, b) => new Date(b.createTime) - new Date(a.createTime));
							this.khzyData = dataArray.filter(item => item.releaseType === '1')
								.sort((a, b) => new Date(b.createTime) - new Date(a.createTime));
							this.ltData = dataArray.filter(item => item.releaseType === '2')
								.sort((a, b) => new Date(b.createTime) - new Date(a.createTime));
						}
					}
				} catch (error) {
					console.error('获取数据失败:', error);
					uni.showToast({
						title: '数据加载失败',
						icon: 'none'
					});
				} finally {
					this.loading = false; // 结束加载
				}
			},
			// 切换
			onClickItem(index) {
				const currentIndex = Number(index.currentIndex);
				if (this.current !== currentIndex) {
					this.current = currentIndex;
					console.log(this.current)
					this.rczjData = []
					this.khzyData = []
					this.ltData = []
					this.init()
				}
			},
			// 处理编辑
			handleEdit(item) {
				uni.navigateTo({
					url: `/pages/publish/publish?id=${item.guid}&releaseType=${item.releaseType}`
				});
			},
			// 处理已成交
			async handleYcj(item) {
				console.log('item', item)
				let data = {
					...item,
					releaseStatus: '已成交'
				}
				const res =  await this.$api.infoKfupdate(data);
				if (res.code === 212001) {
					uni.showToast({
						title: '该需求已成交',
						icon: 'success'
					})
					this.init()
				}
			},
			// 处理特急
		async handleTj(item) {
				console.log('item', item)
				let data = {
					...item,
					urgentStatus: !item.urgentStatus
				}
				const res =  await this.$api.infoKfupdate(data);
				const action = item.urgentStatus ? '取消' : '成为';
				if (res.code === 212001) {
					uni.showToast({
						title: `已${action}特急`,
						icon: 'success'
					})
					this.init()
				}
			},
			// 处理删除
			handleDelete(item, tab) {
				console.log('删除', tab)
				uni.showModal({
					title: '提示',
					content: '确定要删除这条信息吗？',
					success: async (res) => {
						if (res.confirm) {
							try {
								let data = {
									...item,
									releaseStatus: '已删除'
								}
								let result;
								// 根据tab值调用不同的接口
								if (tab === '论坛') {
									result = await this.$api.infoLtupdate(data);
								} else if (tab === '需求广场') {
									result = await this.$api.infoKfupdate(data);
								} else if (tab === '人才自荐') {
									result = await this.$api.infoZjupdate(data);
								}
								
								if (result.succ) {
									uni.showToast({
										title: '删除成功',
										icon: 'success'
									});
									// 重新加载数据
									this.init();
								} else {
									uni.showToast({
										title: '删除失败',
										icon: 'none'
									});
								}
							} catch (error) {
								console.error('删除失败:', error);
								uni.showToast({
									title: '删除失败',
									icon: 'none'
								});
							}
						}
					}
				});
			}
		}
	}
</script>

<style lang="less">
	.my-publish {
		height: 100%;
		display: flex;
		flex-direction: column;
		background: linear-gradient(180deg, #FAEBDB 0%, rgba(255, 247, 238, 0) 500rpx);

		.segmented-control {
			position: relative;
			height: 100rpx !important;

			&::after {
				z-index: -1;
				content: '';
				position: absolute;
				left: 0;
				right: 0;
				bottom: 0;
				height: 1rpx;
			}

			.segmented-control__text {
				padding: 15px 10rpx !important;
				font-size: 32rpx !important;
			}

			.segmented-control__item--text {
				position: relative;

				&::after {
					z-index: 2;
					content: '';
					position: absolute;
					left: 10rpx;
					right: 10rpx;
					bottom: -1px;
					height: 4rpx;
					background: #AC6C17;
				}
			}
		}

		.content {
			margin: 10rpx 30rpx;
			flex: 1;
			overflow: auto;

			// 标签
			.row-tag-title {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin: 10rpx 0 20rpx;

				&-left {
					color: #AC6C17;
					font-size: 32rpx;

					.uni-icons {
						margin-right: 5rpx;
					}
				}

				&-right {
					font-size: 30rpx;
				}
			}
		}
	}
</style>